import requests
from confing import BASE_URL


# 定义封装接口类


class Reg_Login(object):

    # 实力属性
    def __init__(self, session):
        # 定义session属性,创建接口类对象是传递进来
        self.__session = session  # session = requests.Session()
        self.__img_code_url = BASE_URL + "/common/public/verifycode1/{}"  # 图片验证码
        self.__phone_code_url = BASE_URL + "/member/public/sendSms"  # 获取短信验证码
        self.__reg_url = BASE_URL + "/member/public/reg"  # 注册
        self.__login_url = BASE_URL + "/member/public/login"  # 登录
        self.__status_url = BASE_URL + "/member/public/islogin"  # 获取登录状态

    # 定义接口方法
    def get_img_code(self, random_num):
        # 获取图片验证码接口
        # 发送requests请求
        res = self.__session.get(url=self.__img_code_url.format(random_num))
        # 返回相应结果
        return res

    def get_phone_code(self, req_data):
        # 获取短信验证码接口
        # 请求头
        header = {"Content-Type": "application/x-www-form-urlencoded"}
        # 发送请求,此处用session对象发送post请求(不考虑cookie变化的问题)
        res = self.__session.post(url=self.__phone_code_url, headers=header, data=req_data)
        # 返回结果
        return res

    def register(self, req_data):
        # 注册号接口
        res = self.__session.post(url=self.__reg_url, data=req_data)
        return res

    def login(self, req_data):
        # 登录接口
        res = self.__session.post(url=self.__login_url, data=req_data)
        return res

    def get_login_status(self):
        # 获取登录状态接口
        res = self.__session.post(url=self.__status_url)
        return res

if __name__ == '__main__':
    # 定义创建对象
    session = requests.Session()
    a = Reg_Login(session)
    # 调用方法
    reg = a.get_img_code(123)
    data2 = {"phone": "15000045061", "imgVerifyCode": 8888, "type": "reg"}
    reg2 = a.get_phone_code(data2)
    data3 = {"phone": "15000045061", "password": "Aa123456", "verifycode": 8888,
             "phone_code": 666666, "dy_server": "on", "invite_phone": ""}
    reg3 = a.register(data3)
    data4 = {"keywords": "15000045061", "password": "Aa123456"}
    reg4 = a.login(data4)
    data5 = {"keywords": "15000045061"}
    reg5 = a.get_login_status(data5)
    # 打印结果
    print(reg)
    print(reg2.text)
    print(reg3.text)
    print(reg4.text)
    print(reg5.text)
